﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WLW_Electricity_detection
{
    public partial class Form_主页面 : Form
    {
        Form_串口配置 Form_串口配置_obj = new Form_串口配置();
        Form_阈值配置 Form_阈值配置_obj = new Form_阈值配置();

        Thread update_voltage_th = null;
        SynchronizationContext Assistant = null;

        bool LED_flicker = false;
        bool LED_state = false;

        public Form_主页面()
        {
            InitializeComponent();
            Assistant = SynchronizationContext.Current;
            startThread();
            timer_电压.Start();
        }


        public class CustomProgressBar : ProgressBar
        {
            public CustomProgressBar()
            {
                base.SetStyle(ControlStyles.UserPaint, true);
                //...
            }

            //重写baiOnPaint方法
            protected override void OnPaint(PaintEventArgs e)
            {
                SolidBrush brush = null;
                Pen pen;
                Rectangle bounds = new Rectangle(0, 0, base.Width, base.Height);
                //...
                e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 1, 1, bounds.Width - 2, bounds.Height - 2);
                bounds.Height -= 4;
                bounds.Width = ((int)(bounds.Width * (((double)base.Value) / ((double)base.Maximum)))) - 4;
                brush = new SolidBrush(Color.Yellow);
                e.Graphics.FillRectangle(brush, 2, 2, bounds.Width, bounds.Height);
            }
        }




        void startThread() // 线程启动函数
        {
            update_voltage_th = new Thread(update_voltage);
            update_voltage_th.Start();
        }

        void update_voltage()
        {
            while (true)
            {
                if(label_电压.Text != Share.val && Share.val != "")
                    Assistant.Post(update, Share.val); 
                Assistant.Post(Real_time, null);
                Thread.Sleep(100);
            }
        }

        void Real_time(object obj)
        {
            label_实时时间.Text = DateTime.Now.ToString();
        }

        void update(object obj)
        {
            string str = (string)obj;
            label_电压.Text = str;

            chart_电压.Series["电压"].Points.AddXY(Share.val_time, str);

            int index = this.dataGridView_记录电压.Rows.Add();
            this.dataGridView_记录电压.Rows[index].Cells[0].Value = Share.val_time;
            this.dataGridView_记录电压.Rows[index].Cells[1].Value = str;
            this.dataGridView_记录电压.FirstDisplayedScrollingRowIndex = index;

            unpack(str);
            label_时间.Text = Share.val_time;

            progressBar_进度.Maximum = (int)(Share.order * 10);
            if ((int)(Share.order * 10) >= (int)(double.Parse(Share.val) * 10))
            {
                progressBar_进度.Value = (int)(double.Parse(Share.val) * 10);
            }
            else
            {
                progressBar_进度.BackColor = Color.White;
                progressBar_进度.Value = (int)(Share.order * 10);
            }
                
        }
       
        void unpack(string val)
        {
            float num = float.Parse(val);

            if (num >= Share.power_shortage_Q && num < Share.power_shortage)
            {
                LED_flicker = false;
                label_变化灯.ForeColor = Color.Black;
            }
            else if (num >= Share.power_shortage && num < Share.low_energy)
            {
                LED_flicker = false;
                label_变化灯.ForeColor = Color.Yellow;
            }
            else if (num >= Share.low_energy && num < Share.order)
            {
                LED_flicker = false;
                label_变化灯.ForeColor = Color.Green;
            }
            else if (num >= Share.order && num < Share.fully_loaded)
                LED_flicker = true;
        }

        private void Form_主页面_Load(object sender, EventArgs e)
        {

        }

        private void 串口配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form_串口配置_obj.Show();
        }

        private void 阈值配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form_阈值配置_obj.Show();
        }

        private void button_退出_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void timer_电压_Tick(object sender, EventArgs e)
        {
            if (LED_flicker)
            {
                if (LED_state)
                {
                    LED_state = !LED_state;
                    label_变化灯.ForeColor = Color.Red;
                }
                else
                {
                    LED_state = !LED_state;
                    label_变化灯.ForeColor = Color.Black;
                }
            }
        }

        private void progressBar_进度_ControlAdded(object sender, ControlEventArgs e)
        {

        }
    }
}
